﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Windows.Forms;

namespace TCPClient.UI.UserControls
{
    public enum eRedOrGreenSelected
    {
        Neuteral,
        Red,
        Green,
        Blue
    };

    public enum eCurrentState
    {
        Neuteral,
        Red,
        Green
    }

    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class OneShiftUserControl : UserControl
    {
        private DateTime m_StartTime;
        private DateTime m_EndTime;
        private RedGreenSelectionUserControl m_RedGreenUserControl;
        private eRedOrGreenSelected m_ColorSelected = eRedOrGreenSelected.Green;


        public OneShiftUserControl()
        {
            InitializeComponent();
        }

        public eCurrentState CurrentState
        {
            get
            {
                if(pictureBoxPicGreen.Visible)
                {
                    return eCurrentState.Green;
                }
                if(pictureBoxPicRed.Visible)
                {
                    return eCurrentState.Red;
                }
                return eCurrentState.Neuteral;

            }
        }

        public void SetData(DateTime startTime, DateTime endTime)
        {
            m_StartTime = startTime;
            m_EndTime = endTime;
        }

        public DateTime StartTime
        {
            get { return m_StartTime; }
            set { m_StartTime = value; }
        }

        public DateTime EndTime
        {
            get { return m_EndTime; }
            set { m_EndTime = value; }
        }

        public void SetPicVisible(eRedOrGreenSelected picType)
        {
            switch (picType)
            {
                case eRedOrGreenSelected.Green:
                    pictureBoxPicGreen.Visible = true;
                    break;
                case eRedOrGreenSelected.Red:
                    pictureBoxPicRed.Visible = true;
                    break;
                case eRedOrGreenSelected.Blue:
                    pictureBoxBlue.Visible = true;
                    break;
            }
        }

        public void CleanShiftCell()
        {
            pictureBoxPicGreen.Visible = false;
            pictureBoxPicRed.Visible = false;
            pictureBoxBlue.Visible = false;
        }

        private void OneShiftUserControl_MouseEnter(object sender, EventArgs e)
        {
            if (m_RedGreenUserControl.IsEnabledForEditing())
            {
                if (m_ColorSelected == eRedOrGreenSelected.Red)
                {
                    (sender as OneShiftUserControl).BackColor = Color.Red;
                }
                else if (m_ColorSelected == eRedOrGreenSelected.Green)
                {
                    (sender as OneShiftUserControl).BackColor = Color.GreenYellow;
                }
                else if (m_ColorSelected == eRedOrGreenSelected.Neuteral)
                {
                    (sender as OneShiftUserControl).BackColor = SystemColors.ActiveCaption;
                }
            }

        }

        private void OneShiftUserControl_MouseLeave(object sender, EventArgs e)
        {
            (sender as OneShiftUserControl).BackColor = SystemColors.ActiveCaption;
        }

        private void OneShiftUserControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (m_RedGreenUserControl.IsEnabledForEditing())
            {
                if (m_ColorSelected == eRedOrGreenSelected.Neuteral)
                {
                    pictureBoxPicGreen.Visible = false;
                    pictureBoxPicRed.Visible = false;
                }
                else
                {
                    if (pictureBoxPicGreen.Visible && m_ColorSelected == eRedOrGreenSelected.Red)
                    {
                        pictureBoxPicGreen.Visible = false;
                        pictureBoxPicRed.Visible = true;
                    }
                    else if (pictureBoxPicRed.Visible && m_ColorSelected == eRedOrGreenSelected.Green)
                    {
                        pictureBoxPicGreen.Visible = true;
                        pictureBoxPicRed.Visible = false;
                    }
                    else if (!pictureBoxPicGreen.Visible && !pictureBoxPicRed.Visible)
                    {
                        switch (m_ColorSelected)
                        {
                            case eRedOrGreenSelected.Green:
                                pictureBoxPicGreen.Visible = true;
                                break;
                            default:
                                pictureBoxPicRed.Visible = true;
                                break;
                        }
                    }
                }
            }
        }

        public void SetTheGreenRedControlToEachShiftCell(RedGreenSelectionUserControl redGreenSelectionUserControl1)
        {
            m_RedGreenUserControl = redGreenSelectionUserControl1;
            m_RedGreenUserControl.GetRedRadioButton().CheckedChanged += setColorSelectedRed;
            m_RedGreenUserControl.GetGreenRadioButton().CheckedChanged += setColorSelectedGreen;
            m_RedGreenUserControl.GetNeuteralRadioButton().CheckedChanged += setColorSelectedNeuteral;
        }

        private void setColorSelectedRed(object sender, EventArgs e)
        {
            if((sender as RadioButton).Checked)
            {
                m_ColorSelected = eRedOrGreenSelected.Red;
            }
        }

        private void setColorSelectedGreen(object sender, EventArgs e)
        {
            if ((sender as RadioButton).Checked)
            {
                m_ColorSelected = eRedOrGreenSelected.Green;
            }
        }

        private void setColorSelectedNeuteral(object sender, EventArgs e)
        {
            if ((sender as RadioButton).Checked)
            {
                m_ColorSelected = eRedOrGreenSelected.Neuteral;
            }
        }
    }
}
